﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BitcoinMarket.ViewModels;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
using BitcoinMarket.Libs;
using BitcoinMarket.Resources;
using BitcoinMarket.DataConnectors;

namespace BitcoinMarket
{
    public partial class AppSettingsPage : PhoneApplicationPage
    {
        public AppSettingsPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadCurrenciesData()
        {
            this.CurrenciesProgressBar.Visibility = Visibility.Visible;

            BitcoinChartsMarketsDataConnector MarketsConnector = new BitcoinChartsMarketsDataConnector();
            MarketsConnector.GetMarketsData(this.MarketsDataConnectorReady, true);
        }

        /// <summary>
        /// Callback for the markets dataconnector
        /// </summary>
        public void MarketsDataConnectorReady(Collection<BitcoinChartsMarketsCurrencyCollection> DataColl)
        {
            ObservableCollection<AppSettingsCurrencyViewModel> Currencies = new ObservableCollection<AppSettingsCurrencyViewModel>();
            AppCurrencySettings settings = new AppCurrencySettings();

            //Add the parsed data into the UI component
            foreach (BitcoinChartsMarketsCurrencyCollection item in DataColl)
            {
                Currencies.Add(new AppSettingsCurrencyViewModel() { CurrencyTitle = item.Currency, CurrencyTopped = settings.GetValueOrDefault(item.Currency, settings.GetToppedDefault(item.Currency)) });
            }

            ((AppSettingsViewModel)this.DataContext).CurrenciesItems = Currencies;

            this.CurrenciesProgressBar.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Creates a view model for the network status charts list
        /// </summary>
        /// <returns></returns>
        private ObservableCollection<AppSettingsNetworkStatusItemsViewModel> GetNetworkStatusChartsViewModel(Collection<NetworkStatusChartSettingsItem> Charts)
        {
            ObservableCollection<AppSettingsNetworkStatusItemsViewModel> cvm = new ObservableCollection<AppSettingsNetworkStatusItemsViewModel>();

            foreach (NetworkStatusChartSettingsItem chartData in Charts)
            {
                AppSettingsNetworkStatusItemsViewModel vm = new AppSettingsNetworkStatusItemsViewModel();

                vm.ChartKey = chartData.keyName;
                vm.ChartEnabledDefault = chartData.enabledDefault;
                vm.ChartTitle = chartData.chartTitle;
                vm.ChartDescription = chartData.chartDescription;

                cvm.Add(vm);
            }

            return cvm;
        }

        /// <summary>
        /// Navigate to the About page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.IsVisible = true;
            SystemTray.Opacity = 0;
            SystemTray.ForegroundColor = Colors.White;

            this.BuildLocalizedApplicationBar();

            //Load data

            AppNetworkStatusChartSettings settings = new AppNetworkStatusChartSettings();

            AppSettingsViewModel mvm = new AppSettingsViewModel();

            //Set the charts data
            mvm.NetworkStatusChartItems = this.GetNetworkStatusChartsViewModel(settings.GetAllNetworkInfoItems());
            this.DataContext = mvm;

            //Set the currencies data
            this.LoadCurrenciesData();
        }

        /**
         * Builds the application Bar with translations
         */
        private void BuildLocalizedApplicationBar()
        {
            ApplicationBar = new ApplicationBar();
            ApplicationBar.ForegroundColor = Colors.Black;
            ApplicationBar.BackgroundColor = ColorTools.GetColorFromHexa("#FFD8D8D8");
            ApplicationBar.Mode = ApplicationBarMode.Minimized;

            ApplicationBarMenuItem appBarItemAbout = new ApplicationBarMenuItem(AppResources.AboutMenuItem);
            appBarItemAbout.Click += this.ApplicationBarMenuItem_Click;
            ApplicationBar.MenuItems.Add(appBarItemAbout);
        }
    }
}